{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# pipeline要点\n",
    "1. 同时指定model和tokenizer 或者都不指定\n",
    "2. pipeline相当于model head\n",
    "3. 如果不使用pipeline 需要使用`model.config.id2label[pred]`"
   ],
   "id": "77e2d607aa45e434"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import warnings\n",
    "\n",
    "from MyHelper import Config\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import transformers"
   ],
   "id": "bbf97e10e6f12cf2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "所有任务类型",
   "id": "37840a57d2dded72"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "for k, v in transformers.pipelines.SUPPORTED_TASKS.items():\n",
    "    print(k)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "使用默认模型",
   "id": "10e90302801d91b9"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "pipe = transformers.pipeline(\"text-classification\")\n",
    "rt = pipe([\"very good!\", \"vary bad!\"])\n",
    "rt"
   ],
   "id": "964498410a5778f9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "指定模型",
   "id": "9b6119152cdcdac5"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "pipe = transformers.pipeline(\"text-classification\", model=Config.roberta_base_finetuned_dianping_chinese)\n",
    "rt = pipe([\"very good!\", \"vary bad!\"])\n",
    "rt"
   ],
   "id": "78619f64915c8266",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "指定模型和分词",
   "id": "a64b86c3859b1306"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "model = transformers.AutoModelForSequenceClassification.from_pretrained(Config.roberta_base_finetuned_dianping_chinese)\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(Config.roberta_base_finetuned_dianping_chinese)\n",
    "pipe = transformers.pipeline(\"text-classification\", model=model, tokenizer=tokenizer)\n",
    "\n",
    "rt = pipe([\"very good!\", \"vary bad!\"])\n",
    "rt, model, tokenizer, model.config, pipe"
   ],
   "id": "3fc21eac7f2a36eb",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "input_text = \"我觉得不太行！\"\n",
    "input_tensors = tokenizer(input_text, return_tensors=\"pt\").to(Config.device)\n",
    "res = model(**input_tensors)\n",
    "logits = res.logits\n",
    "logits = logits.softmax(dim=-1)\n",
    "pred = logits.argmax(axis=-1).item()\n",
    "lable = model.config.id2label[pred]\n",
    "input_tensors, res, logits, pred, lable"
   ],
   "id": "362e2c7975d5f097",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "查看类型和参数",
   "id": "3a5b3551bd49f465"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "qa_pipe = transformers.pipeline(\"question-answering\")\n",
    "qa_pipe, type(qa_pipe)"
   ],
   "id": "b2e1828e3379bfe8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "rt = qa_pipe({\n",
    "    \"question\": \"What is the capital of France?\",\n",
    "    \"context\": \"The capital of France is Paris.\"\n",
    "})\n",
    "rt"
   ],
   "id": "c8215ccec2014a82",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "图像识别",
   "id": "ef3dccedfc00daa3"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "checkpoint = \"google/owlvit-base-patch32\"\n",
    "detector = transformers.pipeline(model=checkpoint, task=\"zero-shot-object-detection\")"
   ],
   "id": "c447dfdccff089ef",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import requests\n",
    "from PIL import Image\n",
    "\n",
    "url = \"https://unsplash.com/photos/oj0zeY2Ltk4/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTR8fHBpY25pY3xlbnwwfHx8fDE2Nzc0OTE1NDk&force=true&w=640\"\n",
    "im = Image.open(requests.get(url, stream=True).raw)\n",
    "predictions = detector(im, candidate_labels=[\"hat\", \"sunglasses\"], multi_label=True)\n",
    "\n",
    "im, predictions"
   ],
   "id": "f0d8874e5aee75d5",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
